layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer;

    window.addEventListener("keydown",function (e){
        var keyCode = window.event?e.keyCode:e.which;
        if(keyCode==13){
            vm.login();
        }
    })

    // 登录过期的时候，跳出ifram框架
    if (top.location != self.location) top.location = self.location;

    let vm = new Vue({
        el: "#page",
        data: {
            yzm: '',//验证码：js生成
            username:'',//用户名
            password:'',//密码
            captcha:'',//验证码：用户输入
            NewPwd:'',
            ConfirmPwd:'',
        },
        created(){
            var _this = this;
        },
        mounted() {
            this.createCaptcha();
        },
        methods: {
            //生成验证码
            createCaptcha() {
                let that = this;
                let captcha = new Captcha({
                    lineWidth: 1,   //线条宽度
                    lineNum: 3,       //线条数量
                    dotR: 1,          //点的半径
                    dotNum: 10,       //点的数量
                    preGroundColor: [0,1],    //前景色区间
                    backGroundColor: [200,255], //背景色区间
                    fontSize: 60,           //字体大小
                    fontFamily: ['微软雅黑'],  //字体类型
                    fontStyle: 'fill',      //字体绘制方法，有fill和stroke
                    content: 'abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',  //验证码内容
                    length: 5    //验证码长度
                });
                try {
                    // console.log("document.getElementById:",document.getElementById("#captcha1"));
                    // console.log("document.querySelector:",document.querySelector("#captcha1"));
                    captcha.draw(document.querySelector("#captcha1"), code => {
                        that.yzm = code;
                        console.log(code);
                    })
                }catch (e) {
                    console.log(e);
                }
            },
            login(){
                var loading = layer.load(1, {
                    shadeClose: false,
                    title: '登录中..',
                    shade: [0.8,'#fff'] //0.1透明度的白色背景
                });
                let that = this;
                let pwdReg = new RegExp("^(?![a-zA-z]+$)(?!\\d+$)(?![!@#$%^&*]+$)[a-zA-Z\\d!@#$%^&*]+$");
                let reg = /[\`~!@#$%^&*()_+-=<>?:"{},.\\\\/;'[\\]]/im;
                if (this.username == ''){
                    layer.close(loading);
                    layer.msg('用户名不能为空');
                    return false;
                }else if(reg.test(this.username)){
                    layer.close(loading);
                    layer.msg('不能包含非法字符');
                    return false;
                }
                if (this.password == '') {
                    layer.close(loading);
                    layer.msg('密码不能为空');
                    return false;
                }else if(reg.test(this.password)){
                    layer.close(loading);
                    layer.msg('不能包含非法字符');
                    return false;
                }
                if (this.captcha == '') {
                    layer.close(loading);
                    layer.msg('验证码不能为空');
                    return false;
                } else if (this.captcha.toLowerCase() != this.yzm.toLowerCase()) {
                    layer.close(loading);
                    layer.msg('验证码不正确');
                    return false;
                }
                let data = new FormData();
                data.append("mno",that.username);
                data.append("mpwd",that.password);
                data.append("loginip",returnCitySN['cip']);
                data.append("loginarea",returnCitySN['cname']);
                this.$http.post('/future/manager/login',data).then(res=>{
                    let data = res.data.pojo;
                    layer.close(loading);
                    if("000000"==res.data.code){
                        layer.msg("用户名或密码错误",{icon:2,time:3000});
                    }else if("000001"==res.data.code){
                        layer.alert(res.data.message);
                    }else if("111110"==res.data.code){
                        layer.open({
                             type: 1
                            ,title: "修改密码" //不显示标题栏
                            ,closeBtn: false
                            ,area: ['40%','40%']
                            ,shade: 0.8
                            ,btn: ['确认','取消']
                            ,moveType: 1 //拖拽模式，0或者1
                            ,content: $('#openPage')
                            ,success: function(layero){
                                 $("#openPage").removeClass("layui-hide");
                            },
                            yes: function(index, layero){
                                console.log(vm.$data.NewPwd);
                                if(!pwdReg.test(vm.NewPwd)){
                                    layer.alert("密码须为 字母+数字 / 字母+特殊字符 / 数字+特殊字符 组合");
                                    return false;
                                }
                                if(that.ConfirmPwd!==that.NewPwd){
                                    layer.alert("两次密码输入不一致");
                                    return false;
                                }
                                $.ajax({
                                    url: "/future/manager/setPwd",
                                    type: "POST",
                                    data:JSON.stringify({mid:res.data.pojo.mid,mpwd:that.NewPwd}),
                                    contentType:"application/json;charset=utf-8",
                                    dataType:"json",
                                    async:false,
                                    success(res){
                                        console.log("返回码---",res.code);
                                        if("200"!=res.code){
                                            layer.alert(res.message);
                                            return false;
                                        }else{
                                            layer.msg("登录成功",{icon:1,time:3000},()=>{
                                                sessionStorage.setItem("manageInfo",JSON.stringify(data));
                                                window.location.href="/future/api/index/"+res.data.pojo.mid;
                                            });
                                        }
                                    },error(err){
                                        layer.alert(err);
                                    }
                                })
                            },
                            cancel: function(index) {
                                return true;
                            }
                        });
                    }else{
                        layer.msg("登录成功",{icon:1,time:3000},()=>{
                            sessionStorage.setItem("manageInfo",JSON.stringify(res.data.pojo));
                            window.location.href="/future/api/index/"+res.data.pojo.mid;
                        });
                    }
                }).catch(e=>{
                    layer.close(loading);
                    console.log(e);
                })
            }
        }
    })


});